package com.tbs.parkingsaleapp.UI.ParkListPage;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.*;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.tbs.parkingsaleapp.Http.JsonData.ParkResult;
import com.tbs.parkingsaleapp.R;
import com.tbs.parkingsaleapp.SystemManager.SystemDataMananger;
import com.tbs.parkingsaleapp.UI.Adapters.ParkAdapter;
import com.tbs.parkingsaleapp.UI.Adapters.Selectors.BuyableSelector;
import com.tbs.parkingsaleapp.UI.Adapters.Selectors.DefalutSelector;
import com.tbs.parkingsaleapp.UI.Adapters.Selectors.LikeSelector;
import com.tbs.parkingsaleapp.UI.Adapters.Selectors.LowRecommendSelector;
import com.tbs.parkingsaleapp.UI.Adapters.Sorters.DefalutSorter;
import com.tbs.parkingsaleapp.UI.Adapters.Sorters.DownPriceSorter;
import com.tbs.parkingsaleapp.UI.Adapters.Sorters.UPPriceSorter;
import com.tbs.parkingsaleapp.UI.Listenners.FilterButtonClicker;
import com.tbs.parkingsaleapp.UI.ParkDetailActivity;
import com.tbs.parkingsaleapp.UI.UpdateLike.UpdateLikeActivity;

import java.util.Comparator;

public class ParkListFragment extends Fragment {
    public ParkListFragment() {
        // Required empty public constructor
    }
    SystemDataMananger.ISelectPark[] selectors={};
    Comparator<ParkResult>[] comparators=new Comparator[] {new DefalutSorter(),new UPPriceSorter(),new DownPriceSorter()};

    int nowsorter=0,nowselector=0;

    public int getNowselector() {
        return nowselector;
    }

    public void setNowselector(int nowselector) {
        this.nowselector = nowselector;
        updateList();
    }

    public int getNowsorter() {
        return nowsorter;
    }

    public void setNowsorter(int nowsorter) {
        this.nowsorter = nowsorter;
        updateList();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        selectors=new SystemDataMananger.ISelectPark[]{new DefalutSelector(),new LikeSelector(),new BuyableSelector(),new LowRecommendSelector()};

    }

    ListView ls;

    void updateList()
    {
        ls.setAdapter(new ParkAdapter(getContext(),SystemDataMananger.SelectingParking(selectors[nowselector],comparators[nowsorter])));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root= inflater.inflate(R.layout.fragment_park_list, container, false);
        DrawerLayout dl=root.findViewById(R.id.s_dl);
        dl.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        Button ubutton= root.findViewById(R.id.update);
        ubutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(getContext(), UpdateLikeActivity.class);
                startActivity(intent);
            }
        });
        ls=root.findViewById(R.id.total_list);
        ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ParkResult r=ParkAdapter.get(position);
                ParkDetailActivity.ActivityStart(getContext(),r.getPark().getLocation(),r.getPark().getId());
            }
        });
        ImageButton button=root.findViewById(R.id.s_show);
        button.setOnClickListener(new View.OnClickListener() {
            boolean opened=false;
            @Override
            public void onClick(View v) {
                if (!opened)
                    dl.openDrawer(Gravity.RIGHT);
                else
                    dl.closeDrawer(Gravity.RIGHT);
                opened=!opened;
            }
        });

        Spinner spinner=root.findViewById(R.id.sortSelector);
        String text[]={"默认排序","价格升序","价格降序"};
        ArrayAdapter<String> adp=new ArrayAdapter<String>(getContext(),R.layout.support_simple_spinner_dropdown_item,text);
        spinner.setAdapter(adp);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               setNowsorter(position);
               updateList();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        int[] ids={R.id.s_buyable,R.id.s_light,R.id.s_road,R.id.s_sale,R.id.s_under,R.id.s_shop};
        for (int i:ids
             ) {
            Switch s=root.findViewById(i);
            s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                   int ix= Integer.valueOf(buttonView.getTag().toString());
                   LikeSelector likeSelector= (LikeSelector) selectors[1];
                   likeSelector.setFliter(ix,isChecked);
                }
            });
        }
        FilterButtonClicker f=new FilterButtonClicker(this);
        TextView title=root.findViewById(R.id.fli_0);
        title.setOnClickListener(f);

        Button btn=root.findViewById(R.id.fli_1);
        btn.setOnClickListener(f);

        btn=root.findViewById(R.id.fli_2);
        btn.setOnClickListener(f);

        btn=root.findViewById(R.id.fli_3);
        SeekBar bar=root.findViewById(R.id.s_minvalue);
        LowRecommendSelector selector= (LowRecommendSelector)selectors[3];
        bar.setProgress((int) selector.getMinvalue());
        Button finalBtn = btn;
        finalBtn.setOnClickListener(f);
        bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser)
                {
                    selector.setMinvalue(progress);
                    finalBtn.setText("过滤推荐度低于"+progress+"的");
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        // open(Gravity.RIGHT);
        return root;
    }
}